/**
 * 站点共用JS
 */

/**
 * 头部搜索JS
 */
var timeId = null;
var keyIndex = 0;
var keyText = ["粘贴：你想购买的淘宝宝贝名称", "或粘贴：淘宝宝贝网址(http://item.taobao...id=7002269933)", "搜索一下，哇！ 还额外返利10元哦！ (^-^)"];

/**
 * 
 */
function openKeyText() {
	if ( ! isSysKeyText()) {
		clearTimeout(timeId);
		return ;
	}
	
	///
	$("#sTxt").val(keyText[keyIndex++]);
	if (keyIndex >= keyText.length) {
		keyIndex = 0;
	}
	
	///
	timeId = setTimeout(openKeyText, 3000);
}

/**
 * 
 */
function isSysKeyText() {
	var flag = false;
	var tmp = $("#sTxt").val();
	if (tmp != "") {
		for (i=0; i<keyText.length; i++) {
			if (tmp == keyText[i]) {
				flag = true;
			}
		}
	} else {
		flag = true;
	}
	
	return flag;
}

/**
 * [初始化] 搜索框
 */
function initSearch() {
	/// 文本
	openKeyText();
	
	/// 点击“标签”
	$("#Search .tab a").click(function() {
		$("#Search .tab a").removeClass("on");
		$(this).addClass("on");
		
		$("#sHidden").val($(this).attr("type"));
	});
	
	/// 点击“文本框”
	$("#sTxt").focus(function() {
		clearTimeout(timeId);
		if (isSysKeyText()) {
			$("#sTxt").val("");
		}
	}).blur(function() {
		openKeyText();
	});
	
	/// 表单提交
	$("#searchForm").submit(function() {
		var keyword = $("#sTxt").val();
		if (isSysKeyText()) {
			return false;
		}
		return true;
	});
}

/**
 * [初始化] 收藏
 */
function initFavorite() {
	///
	$("#btnFavorite").click(function() {
		var company = "易购GO_淘宝网女装新款_淘宝网商城_淘宝网网上购物";
		var url = "http://www.e35go.com";
		if (document.all) {
			window.external.addFavorite(url, company);
		} else if (window.sidebar) {
			window.sidebar.addPanel(company, url, "");
		}
		return false;
	});
}

/**
 * [初始化] 菜单
 */
function initMenu() {
	///
	$("#Navline li.has_sub").hover(
		function() {
			$(this).addClass("sub_over");
			$("ul.sub", this).show();
		}, function() {
			$(this).removeClass("sub_over");
			$("ul.sub", this).hide();
		}
	);
}

/**
 * [初始化] 淘宝图片
 */
function initPic(tbpItem, tbpShop) {
	if (undefined == tbpItem) tbpItem = "tbpItem";
	if (undefined == tbpShop) tbpShop = "tbpShop";
	
	///
	$("img."+ tbpItem).each(function() {
		var _this = $(this);
		var src = _this.attr(tbpItem);
		src = jQuery.base64Decode(src);
		
		///
		var img = new Image();
		img.onload = function() {
			_this.hide();
			_this.attr("src", src);
			_this.fadeIn("show");
		}
		img.src = src;
	});
	
	///
	$("img."+ tbpShop).each(function() {
		var _this = $(this);
		var src = _this.attr(tbpShop);
		src = "http://logo.taobaocdn.com/shop-logo"+ src;
		
		///
		var img = new Image();
		img.onload = function() {
			_this.hide();
			_this.attr("src", src);
			_this.fadeIn("show");
		}
		img.src = src;
	});
}

/**
 * 动态加载JS
 */
function addDynamicJs(src) {
	var oHead = document.getElementsByTagName('HEAD').item(0);
	var oScript= document.createElement("script");
	oScript.type = "text/javascript";
	oScript.src = src;
	oHead.appendChild( oScript);
}

/**
 * [初始化] 淘宝JS
 */
function initJsTaobao() {
	addDynamicJs("http://a.tbcdn.cn/apps/top/x/sdk.js?appkey=12307845");
}

/// DOM
$(function() {
	initSearch();
	initFavorite();
	initMenu();
});

/// PAGE
window.onload = function() {
	initJsTaobao();
	initPic();
}
